﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Lephone.Data.SqlEntry;
using Lephone.Data;
using CsharpTpl.model;
using CsharpTpl.common;

namespace CsharpTpl.window
{
    public partial class FrmReportManage : Form
    {
        private T_BBXX _BBXX;
        private T_BBCS _BBCS;
        private T_BBSJY _BBSJY;
        private Button btnTJ;
        private Button btnReset;
        private FrmPrintClient _FrmPrintClient;
        //报表参数统计条件
        private Dictionary<int, Control> _BbcsTjtj = new Dictionary<int, Control>();
#region 构造函数
        public FrmReportManage()
        {
            InitializeComponent();

            this.btnTJ = new System.Windows.Forms.Button();
            this.btnReset = new System.Windows.Forms.Button();

            this.btnTJ.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
            this.btnTJ.Location = new System.Drawing.Point(168, 315);
            this.btnTJ.Name = "btnTJ";
            this.btnTJ.Size = new System.Drawing.Size(128, 23);
            this.btnTJ.TabIndex = 35;
            this.btnTJ.Text = "统  计";
            this.btnTJ.UseVisualStyleBackColor = true;

            this.btnReset.FlatStyle = System.Windows.Forms.FlatStyle.Popup;
            this.btnReset.Location = new System.Drawing.Point(302, 315);
            this.btnReset.Name = "btnReset";
            this.btnReset.Size = new System.Drawing.Size(128, 23);
            this.btnReset.TabIndex = 36;
            this.btnReset.Text = "重置参数";
            this.btnReset.UseVisualStyleBackColor = true;

        }
#endregion
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            MainTab.Enabled = false;
            lvBBXX.Columns.Clear();
            lvBBXX.Columns.Add("报表名称");
            lvBBXX.Columns.Add("分类");
            lvBBXX.Columns.Add("顺序号");
            _LoadBBXXList();
            ImageList ilist = new ImageList();
            ilist.ImageSize = new Size(1, 20);
            lvBBXX.SmallImageList = ilist;
            btnTJ.Click += new EventHandler(btnTJ_Click);
        }

        /// <summary>
        /// 统计按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        void btnTJ_Click(object sender, EventArgs e)
        {
            Dictionary<string, string> parms = new Dictionary<string, string>();
            foreach (Control c in _BbcsTjtj.Values)
            {
                switch (c.GetType().Name)
                {
                    case "TextBox":
                        parms.Add(c.Tag.ToString(), c.Text.Trim());
                        break;
                    case "ComboBox":
                        if (((ComboBox)c).SelectedIndex == -1)
                        {
                            MessageBox.Show("请为" + c.Tag.ToString() + "选择一个值");
                            return;
                        }
                        parms.Add(c.Tag.ToString(), ((ComboxItem)((ComboBox)c).SelectedItem).Key);
                        break;
                    case "DateTimePicker":
                        //ToString("yyyyMMddHHmmss")
                        parms.Add(c.Tag.ToString(), ((DateTimePicker)c).Value.ToString("yyyyMMddHHmmss"));
                        break;
                }
            }
            if (_FrmPrintClient == null || _FrmPrintClient.IsDisposed )
            {
                _FrmPrintClient = new FrmPrintClient();
            }
            
            _FrmPrintClient.Init(_BBXX.Id, parms);
            _FrmPrintClient.ShowDialog(this);
            _FrmPrintClient.Dispose();
        }

        private void btnAddNewReport_Click(object sender, EventArgs e)
        {
            _ClearBBXXInput();
            MainTab.Enabled = true;
            MainTab.SelectedIndex = 0;
            _BBXX = T_BBXX.New;
        }
        /// <summary>
        /// 清除所有输入框的内容
        /// </summary>
        /// <returns></returns>
        void _ClearBBXXInput()
        {
            tboxBBMC.Clear();
            tboxMBMC.Clear();
            tboxSXH.Clear();
            tboxGXDY.Clear();
            _LoadFLMC();
            cboxFLMC.SelectedIndex = 0;
        }
        /// <summary>
        /// 加载报表分类列表
        /// </summary>
        /// <returns></returns>
        void _LoadFLMC()
        {
            cboxFLMC.Items.Clear(); 
            DataSet ds = DbEntry.Context.ExecuteDataset(new SqlStatement("select distinct flmc from t_bbxx"));
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                cboxFLMC.Items.Add(dr["FLMC"]);
            }
            if (cboxFLMC.Items.Count==0)
            {
                cboxFLMC.Items.Add("默认分类");
            }
        }
        
        /// <summary>
        /// 加载左侧报表列表
        /// </summary>
        /// <returns></returns>
        void _LoadBBXXList()
        {
            lvBBXX.Items.Clear();
            List<T_BBXX> bbxxs = T_BBXX.FindAll(new OrderBy("SXH,FLMC"));
            ListViewItem item;
            foreach (T_BBXX bbxx in bbxxs)
            {
                item = new ListViewItem(bbxx.BBMC);
                item.Tag = bbxx;
                item.SubItems.Add(bbxx.FLMC);
                item.SubItems.Add(bbxx.SXH.ToString());
                lvBBXX.Items.Add(item);
            }
        }
        

        /// <summary>
        /// 保存报表基本信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private void button3_Click(object sender, EventArgs e)
        {
            if (Utils.NotNull(tboxBBMC, labelForBBMC) && Utils.NotNull(tboxMBMC,labelForMBMC))
            {
                _BBXX.BBMC = tboxBBMC.Text.Trim();
                _BBXX.MBMC = tboxMBMC.Text.Trim();
                _BBXX.GXDY = tboxGXDY.Text.Trim();
                try
                {
                    _BBXX.SXH = int.Parse(tboxSXH.Text.Trim());
                }
                catch (System.Exception ex)
                {
                	_BBXX.SXH=0;
                }
                _BBXX.FLMC = string.IsNullOrEmpty(cboxFLMC.Text)?"默认分类":cboxFLMC.Text.Trim();
                _BBXX.Save();
                MessageBox.Show("保存成功");
                _LoadBBXXList();
            } 
            else
            {
                return;
            }
        }
        
        /// <summary>
        /// TAB页切换的时候触发
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private void MainTab_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (_BBXX.Id == 0)
            {
                e.Cancel = true;
                MessageBox.Show("请先保存新增的报表信息,或者点击左侧的其中一个报表进行修改.");
            }
            else
            {
                switch (MainTab.SelectedIndex)
                {
                    case 1:
                        _ClearBBCSInput();
                        _LoadBBCS();
                        break;
                    case 2:
                        _ClearBBSJYInput();
                        _LoadBBSJY();
                        break;
                    case 3:
                        //加载报表参数为控件
                        List<T_BBCS> bbcss = T_BBCS.Find(CK.K["BBID"] == _BBXX.Id,new OrderBy("SXH"));
                        Label label;
                        TextBox tbox;
                        ComboBox cbox;
                        T_BBCS bbcs;
                        DateTimePicker dtp;
                        DataSet ds;
                        tabPage4.Controls.Clear();
                        //tabPage4.Controls.IndexOfKey()
                        this.tabPage4.Controls.Add(this.btnTJ);
                        this.tabPage4.Controls.Add(this.btnReset);
                        _BbcsTjtj.Clear();
                        //行数
                        int j = 1;
                        for (int i = 0; i < bbcss.Count;i++ )
                        {

                            bbcs = bbcss[i];
                            label = new Label();
                            tabPage4.Controls.Add(label);
                            label.AutoSize = true;
                            label.Name = "Label-"+i;
                            label.Text = bbcs.CSMC;
                            label.Location = new System.Drawing.Point(i%2==0?14:300, 20+(j-1)*39);

                            switch (bbcs.KJLX)
                            {
                                case "文本框":
                                    tbox = new TextBox();
                                    tabPage4.Controls.Add(tbox);
                                    tbox.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                                    tbox.Name = "Input-" + i;
                                    tbox.Tag = bbcs.CSMC;
                                    tbox.Location = new System.Drawing.Point(i % 2 == 0 ? 87 : 373, 16 + (j - 1) * 39);
                                    tbox.Size = new System.Drawing.Size(190, 21);
                                    _BbcsTjtj.Add(i, tbox);
                                    break;
                                case "选择框":
                                    cbox = new ComboBox();
                                    tabPage4.Controls.Add(cbox);
                                    cbox.Name = "Input-" + i;
                                    cbox.Tag = bbcs.CSMC;
                                    cbox.DropDownStyle = ComboBoxStyle.DropDownList;
                                    cbox.Location = new System.Drawing.Point(i % 2 == 0 ? 87 : 373, 16 + (j - 1) * 39);
                                    cbox.Size = new System.Drawing.Size(190, 21);
                                    if (!string.IsNullOrEmpty(bbcs.CSDM))
                                    {
                                        ds = DbEntry.Context.ExecuteDataset(bbcs.CSDM);
                                        foreach (DataRow dr in ds.Tables[0].Rows)
                                        {
                                            cbox.Items.Add(new ComboxItem(dr[0].ToString(), dr[1].ToString()));
                                        }
                                    }
                                    _BbcsTjtj.Add(i, cbox);
                                    break;
                                case "日期框":
                                    dtp = new DateTimePicker();
                                    tabPage4.Controls.Add(dtp);
                                    dtp.Tag = bbcs.CSMC;
                                    dtp.Name = "Input-" + i;
                                    dtp.Location = new System.Drawing.Point(i % 2 == 0 ? 87 : 373, 16 + (j - 1) * 39);
                                    dtp.Size = new System.Drawing.Size(190, 21);
                                    _BbcsTjtj.Add(i, dtp);
                                    break;
                            }
                            if ( (i+1) % 2 == 0)
                            {
                                j++;
                            }
                        }
                        break;
                }
                
            }
        }

        /// <summary>
        /// 点击左侧列表的其中一行报表时触发,加载报表详细信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private void lvBBXX_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lvBBXX.SelectedItems.Count>0)
            {
                _BBXX = (T_BBXX)lvBBXX.SelectedItems[0].Tag;
                MainTab.Enabled = true;
                _InitBBXX();
                _LoadFLMC();
            }
        }
        void _InitBBXX()
        {
            tboxBBMC.Text = _BBXX.BBMC;
            cboxFLMC.Text = _BBXX.FLMC;
            tboxMBMC.Text = _BBXX.MBMC;
            tboxSXH.Text = _BBXX.SXH.ToString();
            tboxGXDY.Text = _BBXX.GXDY;
            MainTab.SelectedIndex = 0;
        }   
#region 报表数据源相关
        void _LoadBBSJY()
        {
            SqlStatement sql = new SqlStatement("select ID 数据源ID,SJYMC 数据源名称,BZSM 备注说明 from t_bbsjy where bbid='"+_BBXX.Id+"'order by sjymc");
            DataSet ds = DbEntry.Context.ExecuteDataset(sql);
            dvBBSJY.DataSource = ds.Tables[0];
        }
        void _ClearBBSJYInput()
        {
            tboxSJYMC.Clear();
            tboxSJYBZSM.Clear();
            tboxSJYDM.Clear();
        }
        private void btnAddNewBBSJY_Click(object sender, EventArgs e)
        {
            _BBSJY = null;
            _ClearBBSJYInput();
        }
        private void btnSaveBBSJY_Click(object sender, EventArgs e)
        {
            if (Utils.NotNull(tboxSJYMC, labelForSJYMC) && Utils.NotNull(tboxSJYDM, labelForSJYDM))
            {
                if (_BBSJY == null)
                {
                    _BBSJY = T_BBSJY.New;
                }
                _BBSJY.SJYMC = tboxSJYMC.Text.Trim();
                _BBSJY.SJYDM = tboxSJYDM.Text.Trim();
                _BBSJY.BZSM = tboxSJYBZSM.Text.Trim();
                _BBSJY.BBID = _BBXX.Id;
                _BBSJY.Save();
                _BBSJY = null;
                _ClearBBSJYInput();
                _LoadBBSJY();
            }
        }
        private void dvBBSJY_SelectionChanged(object sender, EventArgs e)
        {
            if (dvBBSJY.SelectedRows.Count > 0)
            {
                _BBSJY = T_BBSJY.FindById(long.Parse(dvBBSJY.SelectedRows[0].Cells[0].Value.ToString()));
                tboxSJYMC.Text = _BBSJY.SJYMC;
                tboxSJYBZSM.Text = _BBSJY.BZSM;
                tboxSJYDM.Text = _BBSJY.SJYDM;
            }
        }
        private void btnDeleteBBSJY_Click(object sender, EventArgs e)
        {
            if (dvBBSJY.SelectedRows.Count>0)
            {
                if (MessageBox.Show(this, "确认要删除此项记录么?此操作无法恢复.", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    DbEntry.Context.ExecuteNonQuery(new SqlStatement("delete from t_bbsjy where id=" + long.Parse(dvBBSJY.SelectedRows[0].Cells[0].Value.ToString())));
                    _LoadBBSJY();
                }
            }
        }
#endregion
#region 报表参数相关
        /// <summary>
        /// 加载报表对应的参数列表
        /// </summary>
        /// <returns></returns>
        void _LoadBBCS()
        {
            SqlStatement sql = new SqlStatement("select ID 参数ID,CSMC 参数名称,KJLX 控件类型,SXH 顺序号 from t_bbcs where bbid='"+_BBXX.Id+"' order by sxh");
            DataSet ds = DbEntry.Context.ExecuteDataset(sql);
            dvBBCS.DataSource = ds.Tables[0];
        }
        /// <summary>
        /// 报表参数列表,当选中一行时,触发修改
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private void dvBBCS_SelectionChanged(object sender, EventArgs e)
        {
            if (dvBBCS.SelectedRows.Count > 0)
            {
                _BBCS = T_BBCS.FindById(long.Parse(dvBBCS.SelectedRows[0].Cells[0].Value.ToString()));
                tboxCSMC.Text = _BBCS.CSMC;
                cboxKJLX.Text = _BBCS.KJLX;
                tboxCSSXH.Text = _BBCS.SXH.ToString();
                tboxCSDM.Text = _BBCS.CSDM;
            }
        }
        /// <summary>
        /// 新增报表参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private void btnAddNewBBCS_Click(object sender, EventArgs e)
        {
            _BBCS = null;
            _ClearBBCSInput();
        }
        /// <summary>
        /// 保存报表参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private void btnSaveBBCS_Click(object sender, EventArgs e)
        {
            if (Utils.NotNull(tboxCSMC, labelForCSMC) && Utils.NotNull(cboxKJLX, labelForKJLX) && Utils.NotNull(tboxCSSXH, labelForCSSXH))
            {
                if (_BBCS == null)
                {
                    _BBCS = T_BBCS.New;
                }
                _BBCS.CSMC = tboxCSMC.Text.Trim();
                _BBCS.KJLX = cboxKJLX.Text;
                try
                {
                    _BBCS.SXH = int.Parse(tboxCSSXH.Text.Trim());
                }
                catch (System.Exception ex)
                {
                    _BBCS.SXH = 0;
                }
                _BBCS.CSDM = tboxCSDM.Text.Trim();
                _BBCS.BBID = _BBXX.Id;
                _BBCS.Save();
                _BBCS = null;
                _ClearBBCSInput();
                _LoadBBCS();
            }
        }
        /// <summary>
        /// 清除报表参数form输入框内容
        /// </summary>
        /// <returns></returns>
        void _ClearBBCSInput()
        {
            tboxCSMC.Clear();
            tboxCSDM.Clear();
            tboxCSSXH.Clear();
            cboxKJLX.Text = "";
        }
        /// <summary>
        /// 删除选中行的参数
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        /// <returns></returns>
        private void btnDeleteBBCS_Click(object sender, EventArgs e)
        {
            if (dvBBCS.SelectedRows.Count > 0)
            {
                if (MessageBox.Show(this, "确认要删除此项记录么?此操作无法恢复.", "警告", MessageBoxButtons.YesNo) == DialogResult.Yes)
                {
                    DbEntry.Context.ExecuteNonQuery(new SqlStatement("delete from t_bbcs where id=" + long.Parse(dvBBCS.SelectedRows[0].Cells[0].Value.ToString())));
                    _LoadBBCS();
                }
            }
        }
#endregion
    }
}
